package com.lxz.jucdemo.assist;

import java.util.concurrent.CyclicBarrier;

/**
 * @Author: 刘新智
 * @Date: 2025/10/26 10:04
 * @Description: 加法计数器
 * @Version: v1.0
 */
public class CyclicBarrierTest {
    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(7, () -> {
            System.out.println("集齐7颗龙珠，召唤神龙");
        });
        for (int i = 1; i <= 7; i++) {
            final int num = i;
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName() + "收集到第" + num + "颗龙珠");
                try {
                    cyclicBarrier.await();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }, String.valueOf(i)).start();
        }
    }
}

